/**
 *  EKology - An Ecosystem Simulation Framework
 *  Copyright (C) 2008  Aarón Tavío - aaron.tavio at gmail.com - ULPGC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package net.ekology.core.gui;

import net.ekology.ekosystem.Biotope;
import net.ekology.core.datatypes.Attribute;

import org.jdesktop.application.Action;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.*;
import java.awt.*;


/**
 * @author Aarón Tavío - aaron.tavio at gmail.com
 * @version 1.0.0 - 20081019-1700
 */
public class BiotopePane extends javax.swing.JPanel {
    private EKologyView oView;
    private JScrollPane oParentPane;
    private Biotope oBiotope;
    
    /** Creates new form BiotopePane */
    public BiotopePane(EKologyView oView, JScrollPane oParentPane, Biotope oBiotope) {
        initComponents();
        this.oView = oView;
        this.oParentPane = oParentPane;
        this.oBiotope = oBiotope;
        
        this.createPane();
        
        this.oParentPane.setViewportView(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jIDLabel = new javax.swing.JLabel();
        jIDTextField = new javax.swing.JTextField();
        jCreationDateLabel = new javax.swing.JLabel();
        jCreationDateTextField = new javax.swing.JTextField();
        jVariablesPanel = new javax.swing.JPanel();
        jApplyButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();
        jDeleteButton = new javax.swing.JButton();
        jAgentCountLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(net.ekology.EKology.class).getContext().getResourceMap(BiotopePane.class);
        jIDLabel.setText(resourceMap.getString("jIDLabel.text")); // NOI18N
        jIDLabel.setName("jIDLabel"); // NOI18N

        jIDTextField.setEditable(false);
        jIDTextField.setName("jIDTextField"); // NOI18N

        jCreationDateLabel.setText(resourceMap.getString("jCreationDateLabel.text")); // NOI18N
        jCreationDateLabel.setName("jCreationDateLabel"); // NOI18N

        jCreationDateTextField.setEditable(false);
        jCreationDateTextField.setName("jCreationDateTextField"); // NOI18N

        jVariablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Properties"));
        jVariablesPanel.setName("jVariablesPanel"); // NOI18N

        javax.swing.GroupLayout jVariablesPanelLayout = new javax.swing.GroupLayout(jVariablesPanel);
        jVariablesPanel.setLayout(jVariablesPanelLayout);
        jVariablesPanelLayout.setHorizontalGroup(
            jVariablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 275, Short.MAX_VALUE)
        );
        jVariablesPanelLayout.setVerticalGroup(
            jVariablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 38, Short.MAX_VALUE)
        );

        jApplyButton.setMnemonic('a');
        jApplyButton.setText(resourceMap.getString("jApplyButton.text")); // NOI18N
        jApplyButton.setName("jApplyButton"); // NOI18N
        jApplyButton.setOpaque(true);

        jCancelButton.setMnemonic('c');
        jCancelButton.setText(resourceMap.getString("jCancelButton.text")); // NOI18N
        jCancelButton.setName("jCancelButton"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(net.ekology.EKology.class).getContext().getActionMap(BiotopePane.class, this);
        jDeleteButton.setAction(actionMap.get("resetBiotope")); // NOI18N
        jDeleteButton.setName("jDeleteButton"); // NOI18N

        jAgentCountLabel.setText(resourceMap.getString("jAgentCountLabel.text")); // NOI18N
        jAgentCountLabel.setName("jAgentCountLabel"); // NOI18N
        jAgentCountLabel.setVerifyInputWhenFocusTarget(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCreationDateLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCreationDateTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jIDLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jIDTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jApplyButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                        .addComponent(jDeleteButton)))
                .addContainerGap())
            .addComponent(jVariablesPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jAgentCountLabel)
                .addContainerGap(214, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jIDLabel)
                    .addComponent(jIDTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCreationDateLabel)
                    .addComponent(jCreationDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jAgentCountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jVariablesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jDeleteButton)
                    .addComponent(jApplyButton)
                    .addComponent(jCancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void resetBiotope() {
        System.out.printf("BiotopePane.java->resetBiotope();\n");
    }
    
    public void createPane() {
        Attribute[] aAttribute;
        JLabel jLabel;
        JTextField jTextField;
        Box oHBox;
        DateFormat oDateFormat;
        
        
        jIDTextField.setText(oBiotope.getID());
        oDateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:MM:ss:SSS");
        jCreationDateTextField.setText(oDateFormat.format(oBiotope.getLocalDate().getTime()));
        
        jAgentCountLabel.setText("Agents: "+oBiotope.getAgentCount());
        
        jVariablesPanel.setLayout(new BoxLayout(jVariablesPanel,BoxLayout.PAGE_AXIS));
        aAttribute = oBiotope.getAttributes();
        if (aAttribute != null) {
            for(Attribute oAttribute: aAttribute) {
                oHBox = Box.createHorizontalBox();
                jLabel = new JLabel(oAttribute.getName());
                jTextField = new JTextField();
                jTextField.setName(oAttribute.getName());
                jTextField.setText(oAttribute.getValue().toString());

                oHBox.add(jLabel);
                oHBox.add(Box.createRigidArea(new Dimension(5,5)));
                oHBox.add(jTextField);
                jVariablesPanel.add(oHBox);
                jVariablesPanel.add(Box.createRigidArea(new Dimension(5,5)));
            }
            jApplyButton.setEnabled(true);
        }
        else {
            jVariablesPanel.add(new JLabel("No Properties defined."));
            jApplyButton.setEnabled(false);
        }
    }
    
    
    public void updatePane() {
        Box oBox;
        JTextField oTextField;
        
        for (Component oComponent: jVariablesPanel.getComponents()) {
            if (oComponent instanceof Box) {
                oBox = (Box) oComponent;
                for(Component oBoxComponent: oBox.getComponents()) {
                    if (oBoxComponent instanceof JTextField) {
                        oTextField = (JTextField) oBoxComponent;
                        oTextField.setText(oBiotope.getAttribute(oTextField.getName()).getValue().toString());
                    }
                }
            }
        }
        
        jVariablesPanel.updateUI();
    }

    @org.jdesktop.application.Action
    public void applyChanges() {
        Box oBox;
        JTextField oTextField;
        
        for (Component oComponent: jVariablesPanel.getComponents()) {
            if (oComponent instanceof Box) {
                oBox = (Box) oComponent;
                for(Component oBoxComponent: oBox.getComponents()) {
                    if (oBoxComponent instanceof JTextField) {
                        oTextField = (JTextField) oBoxComponent;
                        oBiotope.setAttribute(oTextField.getName(), oTextField.getText());
                    }
                }
            }
        }
        oView.repaintViewport();
    }
    
    
    @org.jdesktop.application.Action
    public void cancelChanges() {
        updatePane();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jAgentCountLabel;
    private javax.swing.JButton jApplyButton;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JLabel jCreationDateLabel;
    private javax.swing.JTextField jCreationDateTextField;
    private javax.swing.JButton jDeleteButton;
    private javax.swing.JLabel jIDLabel;
    private javax.swing.JTextField jIDTextField;
    private javax.swing.JPanel jVariablesPanel;
    // End of variables declaration//GEN-END:variables
    
}
